package com.psm.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.pzone.AppUser;
import com.psm.util.MD5;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by cheam on 15/6/19.
 */
public class SysUser extends Model<SysUser> {

    public static String tableName="sys_user";

    public static final SysUser me = new SysUser();


    /**
     * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
     */
    public Page<SysUser> pageinate(int pageNumber, int pageSize,String keyword,String year) {

        return paginate(pageNumber, pageSize,"select * ", "from sys_user");
    }

    public Record qryLoginUser(Long Id){
        Record user = Db.findFirst("select * from sys_user where id=?",Id);
        return user;
    }

    public Record queryEditUser(Long Id){
        Record user = Db.findFirst("select u.*, d.dep_name from sys_user u,sys_department d where u.dep=d.id and u.id=?",Id);
        return user;
    }

    public Record qryUser(String user_name,String user_pwd,String status){

        String pwd = MD5.getMD5ofStr(user_pwd);
        Record user = Db.findFirst("select * from sys_user where 1=1 and status=? and code=? and pwd = ?",new Object[]{status,user_name, pwd});
        return user;
    }
    
    public Record checkNum(String phone){

        Record p = Db.findFirst("select * from sys_user where 1=1 and phone = ?",new Object[]{phone});
        return p;
    }
    
    public int saveUser(AppUser au){
    	String sql = "insert into sys_user(code,name,pwd,status,phone,sex,desc,utype,regtype)values(?,?,?,?,?,?,?,?,?)";
    	int r = Db.update(sql,new Object[]{au.getPhone(),au.getNickname(),MD5.getMD5ofStr(au.getPwd()),1,au.getPhone(),au.getSex(),"'"+au.getDesc()+"'",au.getUtype(),1});
    	return r;
    }

    public List<Record> qryUserMenu(Long id){
        List<Record> fistMenu = Db.find("select DISTINCT r.* from sys_ref_user_role rur,sys_ref_role_resource rrr,sys_resource r where rur.sys_role_id=rrr.sys_role_id and rrr.sys_resource_id=r.id and rur.sys_user_id=? and r.parent_id=0 order by r.sort", id);
        for(int i=0;i<fistMenu.size();i++){
            Record new_fistMenu = fistMenu.get(i);
            List<Record> second_menu = Db.find("select DISTINCT r.* from sys_ref_user_role rur,sys_ref_role_resource rrr,sys_resource r where rur.sys_role_id=rrr.sys_role_id and rrr.sys_resource_id=r.id and rur.sys_user_id=? and r.parent_id=? order by r.sort",new Object[]{id,new_fistMenu.get("id")});
            new_fistMenu.set("second_menu",second_menu);
            fistMenu.set(i,new_fistMenu);
        }

        return fistMenu;
    }

    public List<Record> qryPermission(Long id){
        List<Record> Menu = Db.find("select r.* from sys_ref_user_role rur,sys_ref_role_resource rrr,sys_resource r where rur.sys_role_id=rrr.sys_role_id and rrr.sys_resource_id=r.id and rur.sys_user_id=? order by r.sort",id);
        return Menu;
    }

    public Map<String,String> get_menu_permission(Long id){
        List<Record> permission = this.qryPermission(id);
        Map<String,String> map_per = new HashMap<String, String>();
        for(Record pp:permission){
            map_per.put("p"+pp.get("id").toString(),"true");
        }

        return  map_per;
    }
}
